﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QL_NOI_TRU.Entities;
using System.Data;

namespace QL_NOI_TRU.DataLayers
{
    class NhanVienDAL
    {
        KetNoiHelper hp = new KetNoiHelper();


        public NhanVien GetNhanVienByID(int ma)
        {
            NhanVien bk = new NhanVien();

            List<NhanVien> list = GetList();
            foreach (NhanVien b in list)
                if (b.MA_NV == ma)
                {
                    bk = b;
                    break;
                }

            return bk;
        }


        public static List<NhanVien> GetList()
        {
            KetNoiHelper db = new KetNoiHelper();
            DataTable dt = db.GetDateTable("SELECT * FROM NHAN_VIEN");

            List<NhanVien> list = new List<NhanVien>();

            foreach (DataRow row in dt.Rows)
            {
                list.Add(new NhanVien(row));
            }

            return list;
        }


        public static List<NhanVien> GetListByName(string keyword)
        {
            KetNoiHelper hp = new KetNoiHelper();
            DataTable dt = hp.GetDateTable(String.Format("SELECT * FROM NHAN_VIEN WHERE MA_NV like '%{0}%' or HOTEN LIKE '%{0}%'", keyword));

            List<NhanVien> list = new List<NhanVien>();

            foreach (DataRow row in dt.Rows)
            {
                list.Add(new NhanVien(row));
            }
            return list;
        }

        public bool Insert(NhanVien bk)
        {
            int count = hp.ExecuteNonQuery(String.Format("INSERT INTO NHAN_VIEN VALUES(N'{0}','{1}','{2}',N'{3}','{4}','{5}','{6}','{7}','{8}',N'{9}',N'{10}')", bk.MA_NV, bk.MA_KHOA, bk.MAPH, bk.HOTEN, bk.SOCMND, bk.GIOITINH, bk.NGAYSINH, bk.DIACHI, bk.SODIENTHOAI, bk.CHUCDANH, bk.CHUCVU));
            return count > 0;
        }

        public bool Update(NhanVien bk)
        {
            int count = hp.ExecuteNonQuery(String.Format("UPDATE NHAN_VIEN SET MA_KHOA = N'{0}',MAPH = N'{1}',HOTEN = N'{2}',SOCMND = '{3}',GIOITINH=N'{4}',NGAYSINH={5},DIACHI=N'{6}',SODIENTHOAI={7},CHUCDANH=N'{8}',CHUCVU=N'{9}' WHERE MA_NV = {10}", bk.MA_KHOA, bk.MAPH, bk.HOTEN, bk.SOCMND, bk.GIOITINH, bk.NGAYSINH, bk.DIACHI, bk.SODIENTHOAI, bk.CHUCDANH, bk.CHUCVU, bk.MA_NV));
            return count > 0;
        }

        public bool Delete(NhanVien bk)
        {
            int count = hp.ExecuteNonQuery("DELETE NHAN_VIEN WHERE BookID=" + bk.MA_NV);
            return count > 0;
        }

    }
}
